'''
Author: thinktanker thinktanker@163.com
Date: 2023-06-15 09:37:41
LastEditors: duliang thinktanker@163.com
LastEditTime: 2023-06-26 19:35:37
FilePath: \sajj\temp\test.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# ************************************************************
# !                         百度识别
# ************************************************************
import requests
import time
from base64 import b64encode  # b64decode
import json
import os

# import json
class baidu():
    def __init__(self,tokenPath:str) -> None:
        with open(tokenPath, 'r') as file:
            self.access_token = file.read()

    def get_text(self, urlType: str, image: str) -> list:
        # general_basic
        while True:
            try:
                url = f'https://aip.baidubce.com/rest/2.0/ocr/v1/{urlType}'
                headers = {"Content-Type": "application/x-www-form-urlencoded"}
                params = {'image': image, 'access_token': self.access_token}
                resp = requests.post(url=url, headers=headers, data=params)
                return json.loads(resp.text)
            except Exception as e:
                print('exception:', e)
                break
                print("获取token")


#                 self.get_token()

if __name__ == "__main__":
    t = time.time()
    b = baidu()
    # txt = b.get_text('http://www.jsswj.com.cn:88/jsswxxSSI/static/map1/0/3/ae06d89652e144f480377ba25c89b043.png?t={}'.format(t))
    with open(r"H:\gitee\sajj\test1.jpeg", 'rb') as img:
        # img_data = b64encode(img.read())
        img_data = b64encode(img.read()).decode("utf8")
        # img_data = (img.read())
    txt = b.get_text("general_basic", img_data)
    print(txt)
    # txt = b.get_text(
    #     "general",
    #     'http://www.jsswj.com.cn:88/jsswxxSSI/static/map1/0/3/ae06d89652e144f480377ba25c89b043.png'
    # )
    print(txt)